package com.youhui.weixin.controller;

import com.youhui.weixin.common.MythConfig;
import com.youhui.weixin.dao.AdminDao;
import com.youhui.weixin.dao.GoodsDao;
import com.youhui.weixin.domain.Admin;
import com.youhui.weixin.service.ExcelRedisService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;

/**
 * Created by https://github.com/kuangcp on 17-10-11  上午9:11
 * 路径路由, 只是采用的session来校验用户是否登录，控制少数几个页面的登录跳转情况，因为逻辑不多，这样的实现较快
 */
@Controller
@RequestMapping("/admin")
@Log4j
public class AdminRouteController implements Serializable {
    @Autowired
    AdminDao adminDao;
    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    ExcelRedisService redisService;

    @RequestMapping("/loginup")
    public String  checkLogin(Model model,String name, String password, HttpServletRequest request){
        ModelAndView view = new ModelAndView();
        Admin admin = adminDao.findAdminByName(name);

        if(admin==null || !password.equals(admin.getPassword())){
            view.setViewName("redirect:login");
            model.addAttribute("status","密码错误");
            return "login";
        }else{
            log.info("登录成功："+admin.toString());
            request.getSession().setAttribute("admin", admin.getAdminId());
            request.getSession().setAttribute("adminName", admin.getName());
            return "redirect:/admin";

        }
    }

    // @RequestMapping() 只能有一个


    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("admin");
        return "redirect:login";
    }
    @RequestMapping()
    public String admin(HttpServletRequest request){
        Long adminId = (Long)request.getSession().getAttribute("admin");
        if(adminId == null){
            return "redirect:/admin/login";
        }else{
            return "admin";
        }
    }

    private String check(HttpServletRequest request, String target){
        Long adminId = (Long)request.getSession().getAttribute("admin");
        if(adminId == null){
            return "redirect:login";
        }else{
            return target;
        }
    }
    @RequestMapping("/imports")
    public String imports(HttpServletRequest request){
        return check(request, "admin/imports");
    }
    @RequestMapping("/goods")
    public String goods(HttpServletRequest request){
        return check(request, "admin/goods");
    }
    @RequestMapping("/reply")
    public String reply(HttpServletRequest request){
        return check(request, "admin/reply");
    }
    @RequestMapping("/config")
    public ModelAndView config(HttpServletRequest request){
        String page =  check(request, "admin/config");
        ModelAndView view = new ModelAndView(page);
        Map<String, String> maps = redisService.readHashInRedis(MythConfig.EXCEL_READ_DEFINE);
        Set<String> keys = maps.keySet();
        for (String key:keys){
            view.addObject(key, maps.get(key));
        }

        maps = redisService.readHashInRedis(MythConfig.TYPE_DICT);
        view.addObject(MythConfig.TYPE_DICT, maps);
        return view;
    }

    @RequestMapping("/gettodayimportcount")
    @ResponseBody
    public int getTedayImportCount() {
        return goodsDao.getTedayImportCount();
    }

    @RequestMapping("/getgoodscount")
    @ResponseBody
    public long getGoodsCount() {
        return goodsDao.getGoodsCount();
    }

    @RequestMapping("/getdelcount")
    @ResponseBody
    public String getDelCount() {
        String count = stringRedisTemplate.opsForValue().get(MythConfig.ADMIN_DEL_COUNT);
        return count;
    }

}
